home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Gray Matter (PalmOS) / Source / grfpatch2.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-24  |  5.0 KB  |  236 lines

  1. /* Gray Matter
  2.  * Written at MacHack 2000 by Allon Stern
  3.  * FREEWARE
  4.  * Feel free to use this code for working your own hacks, or
  5.  * extend the functionality!
  6.  * However, please don't distribute modified versions as "Gray Matter" or
  7.  * under my name.
  8.  * If you have ideas you want me to incorporate into Gray Matter, send me
  9.  * email at allon+palm@radioactive.org
  10.  */
  11.  
  12.  
  13. #include <Pilot.h>
  14. #include <SysEvtMgr.h>
  15. #include <Graffiti.h>
  16. #include <WindowNew.h>
  17.  
  18. /***********************************************************************
  19.  *
  20.  *   Internal Constants
  21.  *
  22.  ***********************************************************************/
  23. #define kHackFileCreator        'GrMr'
  24. #define kHackCodeID                1001
  25.  
  26. /***********************************************************************
  27.  *
  28.  *   Function Prototypes
  29.  *
  30.  ***********************************************************************/
  31.  
  32. char GetScreenValue(DWord *bits, DWord x, DWord y);
  33. void SetScreenValue(DWord *bits, DWord x, DWord y, DWord newvalue);
  34. void AntiAliasPixel(DWord *bits, DWord x, DWord y);
  35.  
  36.  
  37. void    MyScrDrawChars(WinPtr pWindow, SWord xLoc, SWord yLoc, SWord xExtent, SWord yExtent,
  38.                     SWord clipTop, SWord clipLeft, SWord clipBottom, SWord clipRight,
  39.                     const Char * const chars, Word len, FontPtr fontPtr);
  40.                     
  41.  
  42. void    MyScrDrawChars(WinPtr pWindow, SWord xLoc, SWord yLoc, SWord xExtent, SWord yExtent,
  43.                     SWord clipTop, SWord clipLeft, SWord clipBottom, SWord clipRight,
  44.                     const Char * const chars, Word len, FontPtr fontPtr)
  45. {
  46.     void (*oldtrap)(WinPtr, SWord, SWord, SWord, SWord,
  47.                     SWord, SWord, SWord, SWord,
  48.                     const Char * const, Word, FontPtr);
  49.     DWord temp; // for the feature manager call type checking
  50.     Err err;
  51.     Byte data = 0;
  52.     DWord *bits;
  53.     SWord x,y,tox,toy;
  54.     SWord maxLineHeight;
  55.     RectangleType um;
  56.      GDevicePtr theGWorld;
  57.  
  58.     // Get the address of the old routine from HackMaster:
  59.     err = FtrGet(kHackFileCreator,kHackCodeID,&temp);
  60.     ErrFatalDisplayIf(err, "can't get real trap address");
  61.     oldtrap=(void (*)(WinPtr, SWord, SWord, SWord, SWord,
  62.                     SWord, SWord, SWord, SWord,
  63.                     const Char * const, Word, FontPtr))temp;
  64.  
  65.  
  66.     oldtrap (pWindow, xLoc, yLoc, xExtent, yExtent,
  67.                     clipTop, clipLeft, clipBottom, clipRight,
  68.                     chars, len, fontPtr);
  69.     
  70.     theGWorld = pWindow->gDeviceP;
  71.     bits = (DWord *)theGWorld->baseAddr;
  72.     maxLineHeight = fontPtr->fRectHeight;
  73.     x = xLoc;
  74.     y = yLoc;
  75.     tox = xExtent + xLoc;
  76.     toy = yExtent + yLoc;
  77.  
  78. /*
  79. typedef struct {
  80.   SWord x;
  81.   SWord y;
  82. } PointType;
  83.  
  84.  
  85. typedef struct {
  86.   PointType  topLeft;
  87.   PointType  extent;
  88. } RectangleType;
  89. */
  90.     um.topLeft.x = x;
  91.     um.topLeft.y = y;
  92.     um.extent.x = xExtent;
  93.     um.extent.y = yExtent;
  94. //    WinDrawRectangle(&um, 3);
  95.  
  96.  
  97.     if (theGWorld->pixelSize == 1) {
  98.         DWord i,j;
  99.         for (i = x; i <= tox; i++) {
  100.             for (j = y; j <= toy; j++) {
  101.                 DWord bitnum;
  102.                 DWord value;
  103.                 DWord mask;
  104.                 bitnum = 160 * j + i;
  105.                 mask = 0x00000001L << (31 - bitnum % 32);
  106.                 value = bits[(bitnum / 32)] & mask;
  107.                 if (value)
  108.                     bits[bitnum/32] &= ~mask;
  109.                 else
  110.                     bits[bitnum/32] |= mask;
  111.             }
  112.         }
  113.     } else {
  114.         DWord i,j;
  115.         for (i = x; i <= tox; i++) {
  116.             for (j = y; j <= toy; j++) {
  117.             
  118.  
  119. /*
  120.                 DWord bitnum;
  121.                 DWord value;
  122.                 DWord mask;
  123.                 bitnum = 160 * j + i;
  124.                 mask = 0x00000003L << (30 - (bitnum % 16)*2 );
  125.                 value = bits[(bitnum / 16)] & mask;
  126. //                if (value)
  127. */
  128. //                if (!GetScreenValue(bits, i, j))
  129. //                    SetScreenValue(bits, i, j, 0x01);
  130. /*
  131.                     bits[bitnum/16] &= ~mask;
  132.                 else
  133.                     bits[bitnum/16] |= mask;
  134.  
  135. */
  136.             if (!GetScreenValue(bits, i, j))
  137.                 AntiAliasPixel( bits, i, j);
  138.  
  139.             }
  140.         }
  141.         
  142.     }
  143.         
  144.         
  145.         
  146. }
  147.  
  148. void AntiAliasPixel(DWord *bits, DWord x, DWord y)
  149. {
  150.     DWord total = 0;
  151.     DWord top = 0;
  152.     DWord left = 0;
  153.     DWord bottom = 0;
  154.     DWord right = 0;
  155.     
  156.         
  157.     if (GetScreenValue(bits, x-1, y-1) ==0x03) {
  158.         total++;
  159.         top++;
  160.         left++;
  161.     }
  162.  
  163.     if (GetScreenValue(bits, x-1, y) ==0x03) {
  164.         total++;
  165.         top++;
  166.     }
  167.  
  168.     if (GetScreenValue(bits, x-1, y+1) ==0x03) {
  169.         total++;
  170.         top++;
  171.         right++;
  172.     }
  173.  
  174.     if (GetScreenValue(bits, x, y-1) ==0x03) {
  175.         total++;
  176.         left++;
  177.     }
  178.  
  179.     if (GetScreenValue(bits, x, y+1) ==0x03) {
  180.         total++;
  181.         right++;
  182.     }
  183.  
  184.     if (GetScreenValue(bits, x+1, y-1) ==0x03) {
  185.         total++;
  186.         bottom++;
  187.         left++;
  188.     }
  189.  
  190.     if (GetScreenValue(bits, x+1, y) ==0x03) {
  191.         total++;
  192.         bottom++;
  193.     }
  194.  
  195.     if (GetScreenValue(bits, x+1, y+1) ==0x03) {
  196.         total++;
  197.         bottom++;
  198.         right++;
  199.     }
  200.         
  201.     if ((top && bottom) || (left && right))
  202.         return;
  203.     
  204.     if ((top > 1) || (bottom > 1) || (left > 1) || (right > 1)) {
  205.         if (total >= 3)
  206.             SetScreenValue(bits, x, y, 0x01);
  207.         return;
  208.     }
  209.         
  210.     if (total == 2)
  211.         SetScreenValue(bits, x, y, 0x01);
  212.     
  213. }
  214.  
  215. char GetScreenValue(DWord *bits, DWord x, DWord y)
  216. {
  217.     DWord bitnum;
  218.     DWord value;
  219.     DWord mask;
  220.     bitnum = 160 * y + x;
  221.     mask = 0x00000003L << (30 - (bitnum % 16)*2 );
  222.     value = ((bits[(bitnum / 16)]) >> (30 - (bitnum % 16)*2)) & 0x03;
  223.     return value;
  224. }
  225.  
  226. void SetScreenValue(DWord *bits, DWord x, DWord y, DWord newvalue)
  227. {
  228.     DWord bitnum;
  229.     DWord mask;
  230.     
  231.     bitnum = 160 * y + x;
  232.     mask = 0x00000003L << (30 - (bitnum % 16)*2 );
  233.     bits[(bitnum/16)] &= ~mask;
  234.     bits[(bitnum/16)] |= (newvalue & 0x00000003L) <<(30 - (bitnum % 16)*2);
  235. }
  236.